<?php
	// IMPRESCINDIBLE PARA MANEJAR SESIÓN
	session_start();
	
	if (! ($conexion = mysql_connect("localhost","root","root")))
	{
		echo "No se pudo conectar a mysql: " . mysql_error();
	}

	// Seleccionar BD
	if (! mysql_select_db("labolsa_ganuza"))
	{
		echo "No se pudo conectar a minilibreta: " . mysql_error();
	}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es" dir="ltr">
    <head>
       <title>Plantilla xHTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="keywords" content="bolsa,ibex35,juego" />
 <meta name="description" content="Juego Bolsa" />
 <meta name="author" content="Mikel Ganuza" />
 <meta name="generator" content="scite"/>

      <link rel="icon" href="/favicon.ico" type="image/x-icon" />
      <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        
   </head>

<body bgcolor = "black" text = "red">
<h1>La Bolsa</h1>
<div id = "bloque1">
</div>
<form name = "formulario" method = "post" action = "Ej 5.php">
	<fieldset><legend>Agregar a Libreta</legend>
		<label for = "nombre">Nombre</label><br />
		<input type = "text" name = "nombre" id = "nombre" value = "<?php echo $_POST["nombre"]; ?>" /><br />
		<label for = "telefono">Telefono</label><br />
		<input type = "text" name = "telefono" id = "telefono" value = "<?php echo $_POST["telefono"]; ?>" /><br />
		<input type = "submit" name = "enviar" id = "enviar" value = "Enviar" />
	</fieldset>
</form>
<?php
	if ($_POST["enviar"] == "Enviar")
	{
		$nombre = $_POST["nombre"];
		$telf = $_POST["telefono"];
		$boton = $_POST["enviar"];
		$insert = "insert into telefonos(Nombre, Telefono) values ('$nombre' ,'$telf')";
		mysql_query($insert);
	}
	if ($_POST["eliminar"] == "Eliminar")
	{
		$contactos	 = $_POST["contactos"];
		$sql = "delete from telefonos where nombre = '$contactos'";
		mysql_query($sql);
	}
	$sql = "select Nombre from telefonos";
	$resultado = mysql_query($sql);
	$xhtml = "<form method = 'post' action = 'Ej 5.php'><fieldset><legend>Libreta</legend>";
	$xhtml .= "<label for = 'contactos'>Contactos</label><br /><select name = 'contactos' id = 'contactos'>";
	while ($registro = mysql_fetch_assoc($resultado))
	{
		$xhtml .= "<option>" . $registro["Nombre"] . "</option>";
	}
	$xhtml .= "</select><input type = 'submit' name = 'eliminar' id = 'eliminar' value = 'Eliminar' /></fieldset></form>";
	echo $xhtml;
	mysql_close();
      ?>
</div>
</body>
</html>